/**author:yebinx email:yebinx@21cm.com*/
export default class FrameAnimmationBase extends Laya.Sprite{
	public skinUrl:string;
	public sourceImg:Laya.Image;
	public bodyImg:Laya.Image;
	public currIndex:number=0;
	protected endFunc:Function;
	protected frameW:number;
	protected frameH:number;
	protected row:number;
	protected col:number;
	protected allFrames:Array<any>;
	protected anims:any={};
	private currFrames=[];
	private index:number=0;
	private frameIndex:number=0;
	private assetsIsReady:boolean=false;
	private defaultAnim:string="defaultAnim";
	public isLoop:boolean=true;
	public step:number=1;
	private body:Laya.Sprite;
	private misAutoStart:boolean=false;
	private isPlaying:boolean=true;
	private mtimer:Laya.Timer;
	constructor(skinUrl,w:number,h:number,row:number,col:number){
		super();
		this.frameW=w;
		this.frameH=h;
		this.row=row;
		this.col=col;
		this.mtimer=new Laya.Timer();
		this.loadImg(skinUrl)
	}
	
	private loadImg(_url:string){
	    Laya.loader.load(_url,new Laya.Handler(this,(_texture:Laya.Texture)=>{
	        this.initFrames(_texture);
	    }));
	}
	
	public initFrames(_texture:Laya.Texture){
	    this.allFrames=[];
	    let ttexture:Laya.Texture;
	    let tindex=0;
	    let tarr=[];
	    for(let row=0;row<this.row;row++){
	        for(let col=0;col<this.col;col++){
	            ttexture = Laya.Texture.createFromTexture(_texture,col*this.frameW,row*this.frameH,this.frameW,this.frameH);
				ttexture.width=this.frameW;
				ttexture.height=this.frameH;
	            this.allFrames.push(ttexture);
	            tarr.push(tindex);
	            tindex++;
	        }
	    }
	    this.frameIndex=0;
	    this.anims[this.defaultAnim]=tarr;
	    this.currFrames=tarr;
	    this.assetsIsReady=true;
	    this.updateSkin();
		if(this.misAutoStart)this.play();
	}
	
	private updateSkin(){
	    if(!this.body){
	        this.body = new Laya.Sprite();
	        this.addChild(this.body);
	    }
	  
	    let tskin = this.allFrames[this.currFrames[this.frameIndex]];
	    this.body.texture = tskin;
	}
	
	public update(){
	    if(this.assetsIsReady && this.isPlaying){
	        if(this.index%this.step==0){
	            if(this.frameIndex>=this.currFrames.length){
	                this.frameIndex=0;
	                if(this.endFunc)this.endFunc();
					if(!this.isLoop)this.stop();
	            }
	            this.updateSkin();
	            this.frameIndex++;
	        }
	        this.index++;
	    }
	}
	
	public addAnim(name:string,frames:Array<number>){
	    this.frameIndex=0; 
	    this.anims[name]=frames;
	}
	
	public play(name:string="defaultAnim",_delta:number=60){
	    if(this.assetsIsReady){
	        if(this.anims[name])this.currFrames=this.anims[name];
	    }
	    this.isPlaying=true;
	    this.frameIndex=0;
		this.start(_delta);
	}

	private start(_delta:number=60){
		this.mtimer.loop(_delta,this,this.timerRun);
	}

	private timerRun(){
		this.update();
	}
	
	public stop(){
	    this.isPlaying=false;
		this.mtimer.pause();
	}
	
	public setPlayEndFunc(func:Function){
	    this.endFunc=func;
	}
	
	public dispose(){
		this.removeSelf();
		this.anims=null;
		for(let i=0;i<this.allFrames.length;i++){
			this.allFrames[i]=null;
		}
		this.allFrames=null;
		this.mtimer.clear(this,this.update);
		this.mtimer=null;
	}
	
}